//
//  xsChartletView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/29.
// 贴图
// @[@"热门",@"指引",@"装饰",@"人物",@"氛围",@"大头",@"遮挡",@"边框"]

#import "xsChartletView.h"
  
#define xrandomColor  [UIColor colorWithRed:(arc4random() %255)/255.0f green:(arc4random() %255)/255.0f blue:(arc4random() %255)/255.0f alpha:1.0f]

@interface xsChartletView()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *gouButton;// ✅按钮


@property (nonatomic, strong) UIScrollView *bgScrollView;
@property (nonatomic, strong) NSArray *buttons;
@property (nonatomic, strong) UIView *xsContentView;

@property (nonatomic,strong)xsChartletCollectionView *DigitalRobotCollectionView;

@end

@implementation xsChartletView



- (instancetype)initWithButtonTitles:(NSArray *)buttonTitles{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        
        
        [self addSubview:self.topView];
        
        [self.topView addSubview:self.searchButton];
        [self.topView addSubview:self.topTitleLabel];
        [self.topView addSubview:self.gouButton];
        
        [self addSubview:self.bgScrollView];
            
        [self.bgScrollView addSubview:self.xsContentView];
        
        
        [self addSubview:self.DigitalRobotCollectionView];
            
            // 创建按钮数组
        self.buttons = buttonTitles;//@[@"Tab1", @"Tab2", @"Tab3"];
            
          
        
    }
    return self;
}


- (UIView *)xsContentView{
    if(!_xsContentView){
        _xsContentView = [[UIView alloc] init];
        _xsContentView.backgroundColor = UIColor.blackColor;
    }
    return _xsContentView;
}


//@property (nonatomic,strong) UIScrollView *bgScrollView;
//[self.bgView addSubview:self.bgScrollView];
//[self.bgScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
//    make.edges.equalTo(self.bgView);
//}];
- (UIScrollView *)bgScrollView
{
    if(!_bgScrollView){
        _bgScrollView = [[UIScrollView alloc] init];
        _bgScrollView.backgroundColor = UIColor.clearColor;
        
        _bgScrollView.delegate = self;
        _bgScrollView.pagingEnabled = YES;
        _bgScrollView.showsHorizontalScrollIndicator = NO;
    }
    return _bgScrollView;
}



-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
   
}
- (void)xsChangeFrame{
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(50);
    }];
    
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.topView.mas_left);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.gouButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.topView.mas_right);//.offset(10);
        make.centerY.equalTo(self.topView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    
    
    self.bgScrollView.frame = CGRectMake(0, 50+10, self.bounds.size.width, 40);
    
    
    [self.DigitalRobotCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.bgScrollView.mas_bottom).offset(10);
        make.right.equalTo(self.mas_right).offset(-10);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];
    
    
    
    UIFont *btnFont = [UIFont systemFontOfSize:16];
    
    
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < self.buttons.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *buttonTitleStr = self.buttons[i];
        
        CGFloat buttonW = [buttonTitleStr calculateWidth:btnFont withHeight:40]+25.0;
        
//        button.frame = CGRectMake(i * buttonW, 0, buttonW, 40);
        if(lastButton){
            button.frame = CGRectMake(CGRectGetMaxX(lastButton.frame), 0, buttonW, 40);
            
        }
        else{
            button.frame = CGRectMake(0, 0, buttonW, 40);
        }
        button.backgroundColor = UIColor.clearColor;//xrandomColor;
        [button setTitle:self.buttons[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.font = btnFont;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.xsContentView addSubview:button];
        lastButton = button;
    }

    // 设置内容视图的尺寸
    self.xsContentView.frame = CGRectMake(0, 0, CGRectGetMaxX(lastButton.frame), 40);
    self.bgScrollView.contentSize = self.xsContentView.frame.size;
}

- (void)buttonClicked:(UIButton *)sender {
    NSInteger selectedIndex = sender.tag;
    NSLog(@"Selected index: %ld", (long)selectedIndex);
    
    // 在这里切换底部视图
    // ...
}

- (void)updateConstraints{
    [super updateConstraints];
//    
//    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    
    
    
}


- (xsChartletCollectionView *)DigitalRobotCollectionView{
    
    if(!_DigitalRobotCollectionView){
        
        UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
        // 3. 横向间距
        la.minimumInteritemSpacing = 5.0;
        // 4. 纵向间距
        la.minimumLineSpacing = 5.0;
        
        NSMutableArray *tempMarr = [NSMutableArray new];
        for (int i =0; i<10; i++) {
            xsChartletCollectionViewCellModel *md = [xsChartletCollectionViewCellModel new];
            md.iconUrlString = @"bossIPDefaultImage";
            [tempMarr addObject:md];
        }
        
        
        _DigitalRobotCollectionView = [[xsChartletCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:la withData:tempMarr];
        _DigitalRobotCollectionView.backgroundColor = UIColor.blackColor;
        
    }
    return _DigitalRobotCollectionView;
}

- (UIView *)topView{
    
    if(!_topView){
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = UIColor.clearColor;
    }
    return _topView;
}



- (UIButton *)gouButton{
    if (!_gouButton) {
        _gouButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _gouButton.backgroundColor = UIColor.purpleColor;
        [_gouButton addTarget:self action:@selector(gouButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_gouButton setImage:[UIImage imageNamed:@"形状 22"] forState:UIControlStateNormal];
    }
    return _gouButton;
}
- (void)gouButtonAction:(UIButton *)btn{
    NSLog(@"gouButtonAction\n\n");
   
    if([self.delegate respondsToSelector:@selector(xsChartletViewgouButtonBeClick:)]){
        [self.delegate xsChartletViewgouButtonBeClick:btn];
    }
}

- (UIButton *)searchButton{
    if (!_searchButton) {
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = UIColor.purpleColor;
        [_searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_searchButton setImage:[UIImage imageNamed:@"形状 22"] forState:UIControlStateNormal];
    }
    return _searchButton;
}
- (void)searchButtonAction:(UIButton *)btn{
    NSLog(@"searchButtonAction\n\n");
   
    
}
- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = [UIFont systemFontOfSize:21];// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        //_topTitleLabel.text = @"选择数字人";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}



@end
